﻿#region Header
//=============================================================================
//                          GShield (Game Shield)
//=============================================================================
//
// Original Programmer: Shawn Jackson
// Company: WaveTech Digital Technologies, Inc. (http://www.wtdt.com)
// Copyright: (c) 2009 WaveTech Digital Technologies, Inc. All Rights Reserved.
// Assembly: Repositories
// Code File Name: GameRatingRepository.cs
// Code File Date: 1/22/2009
//
// Code File Info: Repo that interacts with Microsoft SQL Data Servers (Azure)
//=============================================================================
/*
 * UPDATES
 * -------
 */
#endregion Header

#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using Microsoft.Samples.Cloud.Data;
using WaveTech.GShield.Model;
using WaveTech.GShield.Model.Data;
using WaveTech.GShield.Repositories.Azure.Entities;

#endregion Includes

namespace WaveTech.GShield.Repositories.Azure
{
	public class GameRatingRepository : IAzureGameRatingRepository
	{
		#region Private ReadOnly Members
		private SsdsContext _context;
		#endregion Private ReadOnly Members

		#region Properties
		public string Container
		{
			get;
			private set;
		}
		#endregion Properties

		#region Constructors/Desctructor
		public GameRatingRepository(string connectionString)
			: this("Metadata", connectionString)
		{

		}

		public GameRatingRepository(string container, string connectionString)
		{
			if (String.IsNullOrEmpty(connectionString))
				throw new ArgumentNullException("connectionString", "Connection String cannot be null");

			this.Container = container;
			this._context = new SsdsContext(connectionString);
			this.EnsureContainer();
		}

		~GameRatingRepository()
		{
			// Finalizer calls Dispose(false)
			this.Dispose(false);
		}
		#endregion Constructors/Desctructor

		#region Ensure Container
		private void EnsureContainer()
		{
			if (!this._context.ContainerExists(this.Container))
			{
				this._context.CreateContainer(this.Container);
			}
		}
		#endregion Ensure Container

		#region Insert Game Rating
		public void InsertGameRating(GameRating gameRating)
		{
			var container = this._context.OpenContainer(this.Container);
			container.Insert(ModelToCloud.GetGameRating(gameRating));
		}
		#endregion Insert Game Rating

		public GameRating GetGameRating(Guid gameRatingId)
		{
			var container = this._context.OpenContainer(this.Container);
			SsdsEntity<CloudGameRating> retrievedGameRating = container.Single<CloudGameRating>(rating => rating.Id == gameRatingId.ToString());
			GameRating gameRating = CloudToModel.GetGameRating(retrievedGameRating);

			return gameRating;
		}

		public void DeleteAllGameRatings()
		{
			var container = this._context.OpenContainer(this.Container);
			var ratings = container.Query<CloudGameRating>(rating => rating.Kind == "CloudGameRating");

			foreach (var v in ratings)
			{
				DeleteEntity(v.Id);
			}
		}

		public void DeleteGameRating(Guid gameRatingId)
		{
			DeleteEntity(gameRatingId.ToString());
		}

		protected void DeleteEntity(string id)
		{
			var container = this._context.OpenContainer(this.Container);
			container.Delete(id);
		}

		#region Dispose
		// Dispose() calls Dispose(true)
		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		// The bulk of the clean-up code is implemented in Dispose(bool)
		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				// free managed resources
				this._context = null;
			}
		}
		#endregion Dispose
	}
}